﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace homework7
{
    public partial class Form1 : Form
    {
        private BackgroundWorker worker;
        private bool isload=false;
        private bool ischanged = false;
        public Form1()
        {
            InitializeComponent();
            
        }
        

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            while (!isload)
                Thread.Sleep(500);
            e.Result = e.Argument;
        }

        private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            HtmlDocument doc = this.web1.Document;
            HtmlElement search = doc.GetElementById("kw");
            HtmlElement button = doc.GetElementById("su");
            search.SetAttribute("value", (string)e.Result);
            button.InvokeMember("click");

            doc = this.web2.Document;
            search = doc.GetElementById("sb_form_q");
            button = doc.GetElementById("sb_form_go");
            search.SetAttribute("value", (string)e.Result);
            button.InvokeMember("click");

            ischanged = true;
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Submit_Click(object sender, EventArgs e)
        {
            worker.RunWorkerAsync(keybox.Text);
            
        }

        private void keybox_TextChanged(object sender, EventArgs e)
        {
            if(((TextBox)sender).Text.Length!=0)
                Submit.Enabled = true;
            else
                Submit.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Submit.Enabled = false;
            worker = new BackgroundWorker();
            worker.DoWork += DoWork;
            worker.RunWorkerCompleted += RunWorkerCompleted;
            this.web1.Visible = false;
            this.web1.ScriptErrorsSuppressed = true;
            this.web1.Navigate("https://www.baidu.com");
            this.web2.Visible = true;
            this.web2.ScriptErrorsSuppressed = true;
            this.web2.Navigate("https://cn.bing.com");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (web1.ReadyState == WebBrowserReadyState.Complete && web2.ReadyState== WebBrowserReadyState.Complete)
            {
                isload = true;
                if (ischanged)
                {
                    HtmlDocument doc = web1.Document;
                    string text = doc.GetElementById("content_left").OuterText;
                    text = text.Substring(0, 200);
                    text1.Text = text;
                    doc = web2.Document;
                    text = doc.GetElementById("b_results").OuterText;
                    text2.Text = text;
                }
            }
            else isload = false;
        }
    }
}
